package br.com.jk.util.parametro;

import br.com.jk.empresa.Empresa;
import br.com.jk.empresa.EmpresaJDialog;
import br.com.jk.empresa.EmpresaRn;
import br.com.jk.util.RNException;
import br.com.jk.util.Util;
import java.awt.Frame;
import java.awt.event.KeyEvent;

/**
 *
 * @author camilo
 */
public class ParametroEmpresaDialog extends javax.swing.JDialog {

  private Empresa empresa;
  private ParametroEmpresa parEmp = new ParametroEmpresa();

  public ParametroEmpresaDialog(Frame parent, boolean modal, ParametroEmpresa parametro) {
    super(parent, modal);
    initComponents();
    this.parEmp = parametro;
  }

  /**
   * Creates new form ParametroDataDialog
   *
   * @param parent
   * @param modal
   */
  public ParametroEmpresaDialog(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
  }

  /**
   * This method is called from within the constructor to initialize the form. WARNING: Do NOT
   * modify this code. The content of this method is always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jlCod = new javax.swing.JLabel();
    jlEmpresa = new javax.swing.JLabel();
    jbSelecionar = new javax.swing.JButton();
    jtfCodEmpresa = new javax.swing.JTextField();
    jtfEmpresa = new javax.swing.JTextField();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Selecione a Empresa");
    setModal(true);
    setResizable(false);
    setType(java.awt.Window.Type.UTILITY);

    jlCod.setFont(new java.awt.Font("Liberation Sans", 1, 12)); // NOI18N
    jlCod.setText("Cod");

    jlEmpresa.setFont(new java.awt.Font("Liberation Sans", 1, 12)); // NOI18N
    jlEmpresa.setText("Empresa");

    jbSelecionar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/jk/imagens/dialog-ok.png"))); // NOI18N
    jbSelecionar.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jbSelecionarActionPerformed(evt);
      }
    });

    jtfCodEmpresa.setFont(new java.awt.Font("Liberation Sans", 0, 12)); // NOI18N
    jtfCodEmpresa.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent evt) {
        jtfCodEmpresaKeyPressed(evt);
      }
    });

    jtfEmpresa.setEditable(false);
    jtfEmpresa.setFont(new java.awt.Font("Liberation Sans", 0, 12)); // NOI18N

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(jtfCodEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jlCod))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addComponent(jlEmpresa)
            .addGap(0, 0, Short.MAX_VALUE))
          .addGroup(layout.createSequentialGroup()
            .addComponent(jtfEmpresa, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jbSelecionar, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addGap(6, 6, 6)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(jlEmpresa, javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(jlCod))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(jbSelecionar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
            .addComponent(jtfCodEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jtfEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    pack();
    setLocationRelativeTo(null);
  }// </editor-fold>//GEN-END:initComponents

  private void jbSelecionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSelecionarActionPerformed
    this.getParametro();
    this.dispose();
  }//GEN-LAST:event_jbSelecionarActionPerformed

  private void jtfCodEmpresaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfCodEmpresaKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
      if (jtfCodEmpresa.getText().isEmpty()) {
        EmpresaJDialog.getInstance(null, true, jtfCodEmpresa, jtfEmpresa).setVisible(true);
      } else {
        try {
          EmpresaRn ern = new EmpresaRn();
          empresa = ern.carregar(Integer.parseInt(jtfCodEmpresa.getText()));
          jtfEmpresa.setText(empresa.getFantasia());
          jbSelecionar.requestFocus();
        } catch (RNException e) {
          Util.msgErro(e, "Erro na consulta da empresa.");
        }
      }
    } else if (evt.getKeyCode() == KeyEvent.VK_F12) {
      EmpresaJDialog.getInstance(null, true, jtfCodEmpresa, jtfEmpresa).setVisible(true);
    }
  }//GEN-LAST:event_jtfCodEmpresaKeyPressed

  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
          javax.swing.UIManager.setLookAndFeel(info.getClassName());
          break;
        }
      }
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
      java.util.logging.Logger.getLogger(ParametroEmpresaDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the dialog */
    java.awt.EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        ParametroEmpresaDialog dialog = new ParametroEmpresaDialog(new javax.swing.JFrame(), true);
        dialog.addWindowListener(new java.awt.event.WindowAdapter() {
          @Override
          public void windowClosing(java.awt.event.WindowEvent e) {
            System.exit(0);
          }
        });
        dialog.setVisible(true);
      }
    });
  }

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton jbSelecionar;
  private javax.swing.JLabel jlCod;
  private javax.swing.JLabel jlEmpresa;
  private javax.swing.JTextField jtfCodEmpresa;
  private javax.swing.JTextField jtfEmpresa;
  // End of variables declaration//GEN-END:variables

  public ParametroEmpresa getParametro() {
    parEmp.setEmpresa(empresa);
    return parEmp;
  }
}
